package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.WatchPageAdapter;
import com.xgkj.diyiketang.data.BuyGuankanquanInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.BuyGuankanquanTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class MyWatchPageActivity extends BaseActivity {
    private PullToRefreshListView mListView;
    private WatchPageAdapter adapter;
    private Context mContext;
    private int buyGuankanquanTask = -1;
    private String quotaId;
    private String currentPage = "1";
    private String size = "10";
    private NoDataUtil noDataUtil;
    private List<BuyGuankanquanInfo.ListBean.BuyDateDetailBean> list;
    private String type;
    private TextView minge1,jihuominge1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_watch_page);
        mContext = this;
        initUI();

    }

    private void initUI() {
        View view1 = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext, view1);
        minge1 = (TextView) findViewById(R.id.zhuanyun_buy);
        jihuominge1 = (TextView) findViewById(R.id.zhuanyun_jihuo);
        mListView = (PullToRefreshListView) findViewById(R.id.watchpage_list);
        adapter = new WatchPageAdapter(this);
        Intent intent = getIntent();
        if (intent.getStringExtra("type")!=null){
            type = intent.getStringExtra("type");
            if (type.equals("parent")){
                String jihuominge = intent.getStringExtra("jihuominge");
                String minge = intent.getStringExtra("minge");
                minge1.setText(minge);
                jihuominge1.setText(jihuominge);
                quotaId = "2737f7a418bf41c998728e6a0f9f253d";//父母
            }else {
                String jihuominge = intent.getStringExtra("jihuominge");
                String minge = intent.getStringExtra("minge");
                minge1.setText(minge);
                jihuominge1.setText(jihuominge);
                quotaId = "3d9513eb1b674af98de226b315765e3f";//转运
            }
        }
        getBuyDetail();
    }

    private void getBuyDetail() {
        BuyGuankanquanTask task = new BuyGuankanquanTask(size, currentPage, quotaId, "1");
        task.addListener(new MyListener(mContext));
        buyGuankanquanTask = getEatShowApp().getTaskManager().addTask(task);
    }

    class MyListener extends ShowToastTaskEventListener {

        private String s;

        public MyListener(Context context) {
            super(context);
        }
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (buyGuankanquanTask == taskId) {
                if (data != null) {
                    BuyGuankanquanInfo info = (BuyGuankanquanInfo) data;
                    buyGuankanquanTask = -1;
//                if (mListView.getVisibility() == View.VISIBLE) {
                    if (currentPage .equals("1")) {
                        adapter.clearData();
                        mListView.onRefreshComplete();
                        mListView.enableLoadMore();
                        mListView.setSelection(0);
                    } else {
                        mListView.onLoadDataComplete(true);
                    }
                    List<BuyGuankanquanInfo.ListBean.BuyDateDetailBean> fansList = new ArrayList<BuyGuankanquanInfo.ListBean.BuyDateDetailBean>();
                    if (data != null) {
                         fansList = ((BuyGuankanquanInfo) data).getList().getList();
                    }
                    if (fansList != null && fansList.size() > 0) {
                        adapter.updateData(fansList);
                    }

                    if (adapter != null && adapter.getCount() > 0) {
                        noDataUtil.ListViewNoEmpty(mListView);
                    } else {
//                    xuanzeLayout.setVisibility(View.VISIBLE);
                        noDataUtil.ListViewEmpty(mListView, null, null, -1);
                    }
                    currentPage = result+"";
//                    list = info.getList().getList();
//                    for (int i = 0; i < list.size(); i++) {
//
//                    }
                }
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    }
}
